package com.qfedu.stussm.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.stussm.dao.ClassDao;
import com.qfedu.stussm.dto.ClaStuDto;
import com.qfedu.stussm.dto.JclassDto;
import com.qfedu.stussm.entity.Classes;
import com.qfedu.stussm.service.intf.ClassService;
import com.qfedu.stussm.vo.PageBeanVo;
import com.qfedu.stussm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ClassServiceImpl implements ClassService {
    @Autowired
    private ClassDao dao;
    @Override
    public R addClass(Classes classes) {
        if (dao.addClass(classes) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }
    @Override
    public PageBeanVo selectAll(String name, int page, int limit) {
        //1.开启分页
        PageHelper.startPage(page, limit);
        if (name != null) {
            name = "%" + name + "%";
        }
        //2.获取查询的结果
        PageInfo<Classes> pageInfo=new PageInfo<>(dao.selectAll(name));
        //3.返回结果
        return new PageBeanVo(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public R delClass(int id) {
        if (dao.delClass(id) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @Override
    public R updateClass(Classes classes) {
        if (dao.updateClass(classes) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @Override
    public R uploadExcel(MultipartFile file) throws IOException {
        //1.获取上传的内容 2.解析文件 3.批量新增
        //1.验证上传文件是否非空
        if(!file.isEmpty()){
            //2.获取上传内容
            InputStream is=file.getInputStream();

            //3.解析Excel
            EasyExcel.read(is,Classes.class,new PageReadListener<Classes>((list)->{
                //4.调用 dao 实现批量新增
                dao.addBatch(list);
            })).sheet().doRead();
        }
        return R.ok();
    }

    @Override
    public void createExcel(HttpServletResponse response) throws IOException {
        //1.查询数据
        List<Classes> list=dao.select();
        //2.设置文件下载
        //设置响应头  告知浏览器，要以附件的形式保存内容   filename=浏览器显示的下载文件名
        response.setHeader("content-disposition","attachment;filename=department_"+System.currentTimeMillis()+".xlsx");
        //3.生成Excel并写出
        EasyExcel.write(response.getOutputStream(),Classes.class).sheet(System.currentTimeMillis()+"").doWrite(list);
    }

    @Override
    public R sel() {
        List<ClaStuDto> list = dao.sel();
        List<Integer> nums = new ArrayList<>(list.size());
        List<String> name = new ArrayList<>(list.size());
        list.forEach(d->{
            nums.add(d.getNums());
            name.add(d.getName());
        });
        Map<String, Object> map = new HashMap<>();
        map.put("nums", nums);
        map.put("name", name);
        return R.ok(map);
    }
}
